package com.webxp.DB;

import java.io.File;
import java.sql.SQLException;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.webxp.config.PropertyFile;

/**
 * 系統的数据库连接类,读取数据库的配置文件，创建连接
 * 
 * @author hdg1988
 * 
 */
public class DBPoolMgr {

	private static org.apache.log4j.Logger logger = org.apache.log4j.Logger
			.getLogger("DBPoolMgr.java");

	/**
	 * 数据库配置文件名
	 */
	private static String configName = "databaseConfig";
	private static String drivername = "com.microsoft.sqlserver.jdbc.SQLServerDriver";

	/**
	 * 数据库的连接信息
	 */
	private static String url = null;
	private static String name = null;
	private static String password = null;

	private static int maxCon;
	private static int minCon;
	private static DBConnectionPool DBPool = null;

	/**
	 * 系统数据库连接初始化
	 * 
	 * @throws SQLException
	 */
	public static void init() {
		if (DBPool != null)
			return;
		// 读取配置文件
		File file = new File(PropertyFile.getProperty(configName));
		Document doc = null;
		try {
			doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
					.parse(file);
		} catch (Exception ex) {
			logger.error("解析数据库配置文件失败！");
			ex.printStackTrace();
			return;
		}
		try {
			DBPool = initDBPool(doc);
		} catch (SQLException e) {
			logger.error("初始化连接池出失败~~", e);
			return;
		}
	}

	/**
	 * 关闭所有数据库连接
	 */
	public static void close() {
		if (DBPool == null)
			return;
		logger.info("正在释放连接...");
		DBPool.release();
		logger.info("释放连接完毕！");
	}

	/**
	 * 读取配置文件
	 * 
	 * @param _doc
	 * @return
	 * @throws SQLException
	 */
	public static DBConnectionPool initDBPool(Document _doc)
			throws SQLException {
		if (DBPool != null)
			return DBPool;
		String connectName = _doc.getDocumentElement().getAttribute(
				"connectName");
		NodeList list_a = _doc.getElementsByTagName("connection");
		NodeList list_b = null;
		for (int i = 0; list_a != null && i < list_a.getLength(); i++) {
			Element elem = (Element) list_a.item(i);
			if (elem.getAttribute("name").equalsIgnoreCase(connectName))
				list_b = elem.getChildNodes();
		}
		if (list_b == null) {
			logger.error("没有找到对应的数据库连接信息！");
		}
		for (int i = 0; i < list_b.getLength(); i++) {
			Node node = list_b.item(i);
			if (node.getNodeName().equalsIgnoreCase("URL"))
				url = node.getChildNodes().item(0).getNodeValue();
			else if (node.getNodeName().equalsIgnoreCase("NAME"))
				name = node.getChildNodes().item(0).getNodeValue();
			else if (node.getNodeName().equalsIgnoreCase("PASSWORD"))
				password = node.getChildNodes().item(0).getNodeValue();
			else if (node.getNodeName().equalsIgnoreCase("MAXCON"))
				maxCon = Integer.parseInt(node.getChildNodes().item(0)
						.getNodeValue());
			else if (node.getNodeName().equalsIgnoreCase("MINCON"))
				minCon = Integer.parseInt(node.getChildNodes().item(0)
						.getNodeValue());
		}
		DBConnectionPool DBPool = new DBConnectionPool("SQLServer", drivername,
				url, name, password, maxCon, minCon);
		logger.info("正在初始化连接池信息...");
		DBPool.init();
		logger.info("初始化连接池信息完成！");
		return DBPool;
	}

	/**
	 * 获取连接池
	 * 
	 * @return
	 */
	public static DBConnectionPool getDBPool() {
		return DBPool;
	}

	/**
	 * 测试
	 * 
	 * @param args
	 * @throws SQLException
	 */
	public static void main(String[] args) throws SQLException {
		PropertyFile.init();
		init();
	}
}
